package com.ihr360.oneclick.service.impl;

import cn.irenshi.meta.exception.IrenshiException;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.hibernate.type.util.StringUtils;
import com.ihr360.oneclick.dao.OneClickTermDao;
import com.ihr360.oneclick.dao.SalaryTypeDao;
import com.ihr360.oneclick.dto.SalaryTypeSettingDto;
import com.ihr360.oneclick.service.SalaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * excel 处理类
 *
 * @author renton
 * @date 18-5-11
 */
@Service
public class SalaryTypeServiceImpl implements SalaryTypeService {

    @Autowired
    private SalaryTypeDao salaryTypeDao;

    @Override
    public void addSalaryType(String companyId, SalaryTypeSettingDto salaryType) {
        List<SalaryTypeSettingDto> salaryTypeSettingDtosDefault = salaryTypeDao.findByCompanyIdAndTypeName("system-default-company-id", salaryType.getTypeName());
        List<SalaryTypeSettingDto> salaryTypeSettingDtos = salaryTypeDao.findByCompanyIdAndTypeName(companyId, salaryType.getTypeName());
        if (CollectionUtils.isNotEmpty(salaryTypeSettingDtos) || CollectionUtils.isNotEmpty(salaryTypeSettingDtosDefault)) {
//            for (SalaryTypeSettingDto salaryTypeSettingDto : salaryTypeSettingDtos) {
//                salaryTypeDao.delete(salaryTypeSettingDto.getId());
//            }

            throw new Ihr360Exception("cn.irenshi.flexible.subset_add_staff_notExit", "新增薪资类型名称不能重复!");
        } else {
            if (salaryType.getTypeName().length() > 12) {
                throw new Ihr360Exception("cn.irenshi.flexible.subset_add_staff_notExit", "新增薪资类型名称不能超过12字!");
            }
            salaryType.setCompanyId(companyId);
            salaryType.setUpdatedAt(new Date());
            salaryType.setCreatedAt(new Date());
            salaryTypeDao.save(salaryType);
        }
    }

    @Override
    @Transactional
    public void editSalaryType(String companyId, SalaryTypeSettingDto salaryType) {
        int flag = 0;
        SalaryTypeSettingDto salaryTypeSettingDto = salaryTypeDao.findOne(salaryType.getId());
        if (salaryTypeSettingDto != null && salaryTypeSettingDto.getTypeName().equals(salaryType.getTypeName())) {
            flag = 1;
        }
        List<SalaryTypeSettingDto> salaryTypeSettingDtos = salaryTypeDao.findByCompanyIdAndTypeName(companyId, salaryType.getTypeName());
        List<SalaryTypeSettingDto> salaryTypeSettingDtosDefault = salaryTypeDao.findByCompanyIdAndTypeName("system-default-company-id", salaryType.getTypeName());
        if (CollectionUtils.isNotEmpty(salaryTypeSettingDtosDefault) && CollectionUtils.isNotEmpty(salaryTypeDao.findByCompanyIdAndReferId(companyId, salaryTypeSettingDtosDefault.get(0).getId()))) {
            flag++;
        }
        if ((salaryTypeSettingDtos.size() + salaryTypeSettingDtosDefault.size()) > flag) {
            throw new IrenshiException("cn.irenshi.flexible.subset_add_staff_notExit", "薪资类型名称不能重复!");
        } else {
            if (salaryType.getTypeName().length() > 12) {
                throw new Ihr360Exception("cn.irenshi.flexible.subset_add_staff_notExit", "薪资类型名称不能超过12字!");
            }
            if (salaryType.getCompanyId().equals("system-default-company-id")) {
                SalaryTypeSettingDto settingDto = new SalaryTypeSettingDto();
                settingDto.setCreatedAt(new Date());
                settingDto.setUpdatedAt(new Date());
                settingDto.setCompanyId(companyId);
                settingDto.setReferId(salaryType.getId());
                settingDto.setEnable(salaryType.getEnable());
                salaryTypeDao.save(settingDto);
            } else {
                salaryType.setCompanyId(companyId);
                salaryType.setUpdatedAt(new Date());
                // salaryTypeDao.delete(salaryType.getId());
                salaryTypeDao.save(salaryType);
            }
        }
    }

    @Override
    @Transactional
    public void deleteSalaryType(String companyId, List<String> idList) {
        List<SalaryTypeSettingDto> salaryTypeSettingDtos = salaryTypeDao.findByCompanyIdOrderByCreatedAtDesc(companyId);
        List<SalaryTypeSettingDto> salaryTypeSettingDtosDefault = salaryTypeDao.findByCompanyIdOrderByCreatedAtDesc("system-default-company-id");
        if (salaryTypeSettingDtos.size() + salaryTypeSettingDtosDefault.size() - idList.size() < 1) {
            throw new Ihr360Exception("cn.irenshi.flexible.subset_add_staff_notExit", "删除失败,薪资类型至少保留一项!");
        } else {
            salaryTypeDao.deleteByIdIn(idList);
        }
    }

    @Override
    public List<SalaryTypeSettingDto> findSalaryType(String companyId) {

        List<SalaryTypeSettingDto> salaryTypeSettingDtos = salaryTypeDao.findByCompanyIdAndReferIdIsNullOrderByCreatedAtDesc(companyId);
        List<SalaryTypeSettingDto> salaryTypeSettingDtosDefault = salaryTypeDao.findByCompanyIdOrderByCreatedAtDesc("system-default-company-id");
        for (SalaryTypeSettingDto salaryTypeSettingDto : salaryTypeSettingDtosDefault) {
            List<SalaryTypeSettingDto> salaryTypeSettingDtoList = salaryTypeDao.findByCompanyIdAndReferId(companyId, salaryTypeSettingDto.getId());
            if (CollectionUtils.isEmpty(salaryTypeSettingDtoList)) {
                salaryTypeSettingDtos.add(salaryTypeSettingDto);
            } else {
                salaryTypeSettingDtos.add(salaryTypeSettingDtoList.get(0));
            }
        }
        Collections.reverse(salaryTypeSettingDtos);
        //salaryTypeSettingDtosDefault.addAll(salaryTypeSettingDtos);
        return salaryTypeSettingDtos;
    }

}
